/* Kanvas Paint and Drawing Tool.
 * Copyright (C) 2011 Darryl Wallace
 * wallacdj@gmail.com / darryl@wallynet.ca
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


/*!
  @file CanvasView.cpp
  @author Darryl Wallace
  @date 2011
  */

#include "CanvasView.h"
#include "math.h"

CanvasView::CanvasView(QWidget *parent)
    : QGraphicsView(parent),
      _currentZoomFactor(1.0),
      _currentAngle(0.0),
      _active(false)
{
    setBackgroundBrush(Qt::gray);
    setResizeAnchor(QGraphicsView::AnchorViewCenter);
}

CanvasView::~CanvasView()
{

}

void CanvasView::wheelEvent(QWheelEvent *event)
{
    if (!_active)
    {
        qDebug()<<"NOT ACTIVE!";
        return;
    }
    /// if control is not held down, then perform the standard
    if (!(event->modifiers() & Qt::ControlModifier))
    {
        QGraphicsView::wheelEvent(event);
        return;
    }

    if (event->modifiers() & Qt::ControlModifier)
    {
        if (event->modifiers() & Qt::ShiftModifier)
        {
            qreal angle;
            if (event->delta()>0)
                angle=_currentAngle + ANGLE_INCREMENT;
            else
                angle=_currentAngle - ANGLE_INCREMENT;
            setViewAngle(angle);
            emit viewAngleChanged(angle);
        }
        else
        {
            qreal scale;
            if (event->delta()>0)
                scale=_currentZoomFactor*WHEEL_NOTCH_FACTOR;
            else
                scale=_currentZoomFactor/WHEEL_NOTCH_FACTOR;

            setZoomFactor(scale);
            emit zoomFactorChanged(scale);
        }
    }
}

void CanvasView::setZoomFactor(qreal zoomFactor)
{
    _currentZoomFactor=zoomFactor;
    QTransform scaleTransform(_currentZoomFactor,0.0,0.0,
                 0.0,_currentZoomFactor,0.0,
                 0.0,0.0,1.0);
    qreal cosa=cos(_currentAngle);
    qreal sina=sin(_currentAngle);
    QTransform rotationTransform(cosa, sina, -sina, cosa, 0, 0);
    setTransform(scaleTransform*rotationTransform);
}

void CanvasView::setViewAngle(qreal angle)
{
    _currentAngle=angle;
    QTransform scaleTransform(_currentZoomFactor,0.0,0.0,
                 0.0,_currentZoomFactor,0.0,
                 0.0,0.0,1.0);
    qreal cosa=cos(_currentAngle);
    qreal sina=sin(_currentAngle);
    QTransform rotationTransform(cosa, sina, -sina, cosa, 0, 0);
    setTransform(scaleTransform*rotationTransform);
}

QTransform CanvasView::scaleTransform() const
{
    return QTransform(_currentZoomFactor,0.0,0.0,
                      0.0,_currentZoomFactor,0.0,
                      0.0,0.0,1.0);
}

void CanvasView::resetZoom()
{
    setZoomFactor(1.0);
}
